package heap;

import global.PageId;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;

public class Serialization {
	private String filename;
	public static String STR = "Data";

	public Serialization(String fileName) {
		filename = fileName;
	}

	public ArrayList<LinkedList<Integer>> read() {
		ArrayList<LinkedList<Integer>> data = null;
		FileInputStream fis = null;
		ObjectInputStream in = null;
		try {
			fis = new FileInputStream(filename + STR);
			in = new ObjectInputStream(fis);
			data = (ArrayList<LinkedList<Integer>>) in.readObject();
			in.close();
		} catch (IOException ex) {
			ex.printStackTrace();
		} catch (ClassNotFoundException ex) {
			ex.printStackTrace();
		}
		return data;
	}

	public void write(ArrayList<LinkedList<Object>> linkedlists) {
		FileOutputStream fos = null;
		ObjectOutputStream out = null;
		try {
			fos = new FileOutputStream(filename + STR);
			out = new ObjectOutputStream(fos);
			out.writeObject(linkedlists);
			out.close();
			System.out.println("Object Persisted");
		} catch (IOException ex) {
			ex.printStackTrace();
		}
	}

	public ArrayList<LinkedList<PageId>> returnFreeAndFull() {
		ArrayList<LinkedList<PageId>> returnedData = new ArrayList<LinkedList<PageId>>();
		ArrayList<LinkedList<Integer>> myData = read();
		for (int i = 0; i < myData.size() - 1; i++) {
			LinkedList<Integer> temp = myData.get(i);
			LinkedList<PageId> ids = new LinkedList<PageId>();
			Iterator<Integer> it = temp.iterator();
			while (it.hasNext()) {
				PageId pid = new PageId();
				pid.pid = it.next();
				ids.add(pid);
			}
			returnedData.add(ids);
		}
		return returnedData;
	}

	public LinkedList<Integer> getRecNumAndCurr() {
		ArrayList<LinkedList<Integer>> myData = read();
		return myData.get(2);

	}

	public static void main(String[] args) {
		Serialization s = new Serialization("amira");
		ArrayList<LinkedList<Object>> linkedlists = new ArrayList<LinkedList<Object>>();
		LinkedList<Object> free = new LinkedList<Object>();
		free.add(9);
		System.out.println(free.get(0));
		free.add(10);
		System.out.println(free.get(1));
		LinkedList<Object> full = new LinkedList<Object>();
		full.add(50);
		System.out.println(full.get(0));
		full.add(99);
		System.out.println(full.get(1));
		LinkedList<Object> l = new LinkedList<Object>();
		l.add(3);
		l.add(4);
		linkedlists.add(free);
		linkedlists.add(full);
		linkedlists.add(l);
		s.write(linkedlists);
		ArrayList<LinkedList<PageId>> linkedlists1 = s.returnFreeAndFull();
		System.out.println(linkedlists1.size());
		LinkedList<Integer> l0 = s.getRecNumAndCurr();
		// ArrayList<LinkedList<PageId>> linkedlists2 =
		// s.returnData(linkedlists1);

		 System.out.println("hhhhhhhhhhhhhhhhhhhhhhh"+linkedlists1.size());
		for (int i = 0; i < linkedlists1.size(); i++) {
			System.out.println("hhhhh"+linkedlists1.get(i).get(0).pid);
		}
		// System.out.println(linkedlists1.get(0).get(1));
	}
}
